package com.shqcjd.sinopecdemomobile.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.model.pojo.EquipmentGroupBean;
import com.shqcjd.sinopecdemomobile.ui.activity.EquipmentAbnormalActivity;
import com.shqcjd.sinopecdemomobile.ui.activity.EquipmentGroupActivity;

import java.util.List;

public class EquipmentGroupStandardAdapter extends BaseQuickAdapter<EquipmentGroupBean.EquipmentStandardBean,BaseViewHolder> {
    private EquipmentGroupActivity context;
    private List<EquipmentGroupBean.EquipmentStandardBean> data;
    private EquipmentGroupBean.EquipmentCheckListBean equipment;
    private int epId, tId;

    static final int VIEW_TYPE_STANDARD = R.layout.item_equipment_group_standard;
    static final int VIEW_TYPE_GROUP = R.layout.item_equipment_group;

    public EquipmentGroupStandardAdapter(@Nullable List<EquipmentGroupBean.EquipmentStandardBean> data, EquipmentGroupBean.EquipmentCheckListBean equipment, EquipmentGroupActivity context, int epId, int tId) {
        super(R.layout.item_equipment_group_standard, data);
        this.context = context;
        this.data = data;
        this.equipment = equipment;
        this.epId = epId;
        this.tId = tId;
    }

    @Override
    protected void convert(BaseViewHolder helper, EquipmentGroupBean.EquipmentStandardBean item) {
        if (helper instanceof StandardViewHolder){
            StandardViewHolder h = (StandardViewHolder) helper;
            h.tv_item_equipment_group_title.setText(item.equipment_type_name);
            h.rv_item_equipment_group.setLayoutManager(new LinearLayoutManager(context));
            EquipmentGroupStandardContentAdapter groupStandardContentAdapter = new EquipmentGroupStandardContentAdapter(item.equipment_type_part, context);
            h.rv_item_equipment_group.setAdapter(groupStandardContentAdapter);
        }else if (helper instanceof GroupViewHolder){
            GroupViewHolder h = (GroupViewHolder) helper;
            h.tv_item_equipment_group_name.setText(equipment.equipment_name);
            h.cb_item_equipment_group_switch.setChecked(equipment.running);
            if(equipment.check_normal == 1){
                h.rb_item_equipment_group_true.setChecked(true);
            }else if(equipment.check_normal == 2){
                h.rb_item_equipment_group_false.setChecked(true);
            }
            h.rv_item_equipment_group.setLayoutManager(new LinearLayoutManager(context));
            EquipmentGroupItemAdapter equipmentGroupItemAdapter = new EquipmentGroupItemAdapter(equipment.equipment_children, context, equipment.equipment_id, tId);
            h.rv_item_equipment_group.setAdapter(equipmentGroupItemAdapter);

            h.cb_item_equipment_group_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        equipmentGroupItemAdapter.setCheckTrue();
                    }else {
                        equipmentGroupItemAdapter.setCheckFalse();
                    }
                }
            });

            h.cb_item_equipment_group_switch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    context.openOrCloseSwitchHttp(equipment.kks);
                }
            });
            context.radioGroupList.add(h.rg_item_equipment_group);
            h.rg_item_equipment_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId==R.id.rb_item_equipment_group_false){
                        Intent it = new Intent(context, EquipmentAbnormalActivity.class);
                        it.putExtra("eId", equipment.equipment_id);
                        it.putExtra("tId", tId);
                        context.startActivityForResult(it, context.END_CODE);
                    }else if(checkedId==R.id.rb_item_equipment_group_true){
                        context.updateTrue(equipment.equipment_id);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    protected int getDefItemViewType(int position) {
        if (position<data.size()-1) {
            return VIEW_TYPE_STANDARD;
        }else{
            return VIEW_TYPE_GROUP;
        }
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        if(viewType==VIEW_TYPE_STANDARD){
            return new StandardViewHolder(LayoutInflater.from(context).inflate(R.layout.item_equipment_group_standard, parent, false));
        }
            return new GroupViewHolder(LayoutInflater.from(context).inflate(R.layout.item_equipment_group, parent, false));
    }

    private class StandardViewHolder extends BaseViewHolder  {

        private RecyclerView rv_item_equipment_group;
        private TextView tv_item_equipment_group_title, tv_item_equipment_group_name;

        StandardViewHolder(View itemView) {
            super(itemView);
            rv_item_equipment_group = itemView.findViewById(R.id.rv_item_equipment_group);
            tv_item_equipment_group_title = itemView.findViewById(R.id.tv_item_equipment_group_title);
        }
    }


    private class GroupViewHolder extends BaseViewHolder {
        private RecyclerView rv_item_equipment_group;
        private TextView tv_item_equipment_group_name;
        private CheckBox cb_item_equipment_group_switch;
        private RadioButton rb_item_equipment_group_true, rb_item_equipment_group_false;
        private RadioGroup rg_item_equipment_group;
        GroupViewHolder(View itemView) {
            super(itemView);
            rv_item_equipment_group = itemView.findViewById(R.id.rv_item_equipment_group);
            tv_item_equipment_group_name = itemView.findViewById(R.id.tv_item_equipment_group_name);
            cb_item_equipment_group_switch = itemView.findViewById(R.id.cb_item_equipment_group_switch);
            rb_item_equipment_group_true = itemView.findViewById(R.id.rb_item_equipment_group_true);
            rb_item_equipment_group_false = itemView.findViewById(R.id.rb_item_equipment_group_false);
            rg_item_equipment_group = itemView.findViewById(R.id.rg_item_equipment_group);
        }
    }
}
